package com.hns.can.master.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.master.entity.SysMsgAlm;
import com.hns.can.master.entity.SysMsgCarspding;
import com.hns.can.master.entity.SysMsgDrvbhv;
import com.hns.can.master.entity.SysMsgWorktime;
import com.hns.can.master.vo.AlarmFaultSettingVo;
import com.hns.can.master.vo.AlarmSettingParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;
/**
 * 报警设置service
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Service
public class AlarmPushSettingService extends CommonService {

	/**
	 * 报警时间设置
	 * @param params
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Transactional
	@CacheEvict(value=GlobalConst.CACHE_ALARM_TIME_SETTING, key="#params.getCorpId()")
	public void setAlarmTime(SysMsgWorktime params) {
		//TODO
		HashMap paramMap = new HashMap();
		paramMap.put("corpId", params.getCorpId());
		this.commonEntityDao.deleteByParamsIgnoreNullValue(SysMsgWorktime.class, paramMap);
		params.setMsgId(UtilString.getUUID());
		params.setCreateTs(new Date());
		params.setCreateUser(this.getCurrentUser().getUser().getId());
		this.commonEntityDao.save(params);
	}

	/**
	 * 查询报警时间设置
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<SysMsgWorktime> searchAlarmTimeSetting(CommonParamsVo params){
		HashMap paramMap = new HashMap();
		paramMap.put("corpId", params.getCorpId());
		List<SysMsgWorktime> list = this.commonEntityDao.findByParamsIgnoreNullValue(SysMsgWorktime.class, paramMap);
		return list;
	}
	
	/**
	 * 行为报警设置
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Transactional
	@CacheEvict(value=GlobalConst.CACHE_ALARM_UNSAFE_SETTING, key="#params.getCorpId()")
	public void setAlarmUnsafe(SysMsgDrvbhv params) {
		// TODO
		HashMap paramMap = new HashMap();
		paramMap.put("corpId", params.getCorpId());
		this.commonEntityDao.deleteByParamsIgnoreNullValue(SysMsgDrvbhv.class, paramMap);
		params.setMsgId(UtilString.getUUID());
		params.setCreateTs(new Date());
		params.setCreateUser(this.getCurrentUser().getUser().getId());
		this.commonEntityDao.save(params);
	}
	
	/**
	 * 查询不安全行为设置
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<SysMsgDrvbhv> searchAlarmUnsafeSetting(SysMsgDrvbhv params) {
		HashMap paramMap = new HashMap();
		paramMap.put("corpId", params.getCorpId());
		List<SysMsgDrvbhv> list = this.commonEntityDao.findByParamsIgnoreNullValue(SysMsgDrvbhv.class, paramMap);
		return list;
	}

	/**
	 * 超速报警设置
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Transactional
	@CacheEvict(value=GlobalConst.CACHE_ALARM_SPEED_SETTING, key="#params.getCorpId()")
	public void setAlarmSpeed(SysMsgCarspding params) {
		HashMap paramMap = new HashMap();
		paramMap.put("corpId", params.getCorpId());
		this.commonEntityDao.deleteByParamsIgnoreNullValue(SysMsgCarspding.class, paramMap);
		params.setMsgId(UtilString.getUUID());
		params.setCreateTs(new Date());
		params.setCreateUser(this.getCurrentUser().getUser().getId());
		this.commonEntityDao.save(params);
	}
	
	
	/**
	 * 查询超速行为设置
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<SysMsgCarspding> searchAlarmSpeedSetting(SysMsgCarspding params) {
		HashMap paramMap = new HashMap();
		paramMap.put("corpId", params.getCorpId());
		List<SysMsgCarspding> list = this.commonEntityDao.findByParamsIgnoreNullValue(SysMsgCarspding.class, paramMap);
		return list;
	}

	/**
	 * 故障报警设置
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@Transactional
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@CacheEvict(value=GlobalConst.CACHE_ALARM_FAULT_SETTING, key="#params.getCorpId()")
	public void setAlarmFault(AlarmSettingParamVo params) {
		HashMap paramMap = new HashMap();
		paramMap.put("corpId", params.getCorpId());
		String[] mftTypes = params.getMftTypes();
		String[] mftDiscCodes = params.getMftDiscCodes();
		if(PubUtil.isNotEmpty(mftTypes)){
			for(int i=0;i<mftTypes.length;i++){
				paramMap.put("corpId", params.getCorpId());
				paramMap.put("mftType", mftTypes[i]);
				paramMap.put("mftDiscCode", mftDiscCodes[i]);
				this.commonEntityDao.deleteByParamsIgnoreNullValue(SysMsgAlm.class, paramMap);
				
				SysMsgAlm entity = new SysMsgAlm();
				entity.setCorpId(params.getCorpId());
				entity.setMftType(mftTypes[i]);
				entity.setMftDiscCode(mftDiscCodes[i]);
				entity.setMsgId(UtilString.getUUID());
				entity.setCreateTs(new Date());
				entity.setCreateUser(this.getCurrentUser().getUser().getId());
				this.commonEntityDao.save(entity);
			}
		}else{
			throw new BusinessException(MsgType.ERROR.getFlag(), "保存失败，请勾选报警的故障");
		}
		
	}
	
	/**
	 * 查询故障报警设置
	 * @param params
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	public Pagination searchAlarmFault(AlarmSettingParamVo params) {
		Pagination page = this.myBatisPage("sysAlarmSetting.getAlarmFaultSettingCnt", "sysAlarmSetting.getAlarmFaultSettingData", params, AlarmFaultSettingVo.class);
		return page;
	}

	@Transactional
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@CacheEvict(value=GlobalConst.CACHE_ALARM_FAULT_SETTING, key="#params.getCorpId()")
	public void setNotAlarmFault(AlarmSettingParamVo params) {
		HashMap paramMap = new HashMap();
		paramMap.put("corpId", params.getCorpId());
		String[] mftTypes = params.getMftTypes();
		String[] mftDiscCodes = params.getMftDiscCodes();
		for(int i=0;i<mftTypes.length;i++){
			paramMap.put("mftType", mftTypes[i]);
			paramMap.put("mftDiscCode", mftDiscCodes[i]);
			this.commonEntityDao.deleteByParamsIgnoreNullValue(SysMsgAlm.class, paramMap);
		}
	}

	

	

	
	
	
}
